home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / bin / ubiquity < prev    next >
Text File  |  2008-10-29  |  5KB  |  133 lines

  1. #! /usr/bin/python
  2.  
  3. # Wrapper script to run Ubiquity as root using the appropriate privilege
  4. # escalation method for the frontend.
  5.  
  6. import sys
  7. import os
  8. import syslog
  9. import subprocess
  10.  
  11. sys.path.insert(0, '/usr/lib/ubiquity')
  12.  
  13. from ubiquity import osextras
  14.  
  15. def main():
  16.     newargv = []
  17.     desktop = None
  18.     frontend = None
  19.     toexec = []
  20.  
  21.     i = 1
  22.     while i < len(sys.argv):
  23.         if sys.argv[i] == '--desktop':
  24.             desktop = sys.argv[i + 1]
  25.             i += 2
  26.             # strip option and argument from newargv
  27.             continue
  28.         elif not sys.argv[i].startswith('-'):
  29.             frontend = sys.argv[i]
  30.         newargv.append(sys.argv[i])
  31.         i += 1
  32.  
  33.     if os.getuid() == 0:
  34.         # no privilege escalation required
  35.         inner = ['/usr/lib/ubiquity/bin/ubiquity']
  36.         inner.extend(newargv)
  37.  
  38.         # Lock HAL's storage subsystem.
  39.         if osextras.find_on_path('hal-lock'):
  40.             toexec.extend(['hal-lock',
  41.                            '--interface', 'org.freedesktop.Hal.Device.Storage',
  42.                            '--exclusive', '--run'])
  43.             # hal-lock has irritating argument passing conventions.
  44.             toexec.append(' '.join(inner))
  45.         else:
  46.             toexec.extend(inner)
  47.     else:
  48.         if frontend is None:
  49.             # Try to detect which frontend will be used by looking for a
  50.             # frontend module.
  51.             import imp
  52.             import ubiquity.frontend
  53.             frontend_names = ['mythbuntu_ui', 'gtk_ui', 'kde_ui']
  54.             for f in frontend_names:
  55.                 try:
  56.                     imp.find_module(f, ubiquity.frontend.__path__)
  57.                     frontend = f
  58.                     break
  59.                 except ImportError:
  60.                     pass
  61.  
  62.         if frontend in ('gtk_ui', 'mythbuntu_ui'):
  63.             toexec = ['gksudo']
  64.             if desktop:
  65.                 toexec.extend(['--desktop', desktop])
  66.             toexec.append('--')
  67.         elif frontend == 'kde_ui':
  68.             kdesu = None
  69.             if osextras.find_on_path('kdesu'):
  70.                 kdesu = 'kdesu'
  71.             elif osextras.find_on_path('kdesudo'):
  72.                 kdesu = 'kdesudo'
  73.             elif os.path.exists('/usr/lib/kde4/bin/kdesudo'):
  74.                 kdesu = '/usr/lib/kde4/bin/kdesudo'
  75.             if kdesu:
  76.                 toexec = [kdesu, '--nonewdcop', '--']
  77.             else:
  78.                 toexec = ['sudo']
  79.             #temporarily force sudo until we work out why kdesudo stops it passing partitioning stage
  80.             toexec = ['sudo']
  81.         else:
  82.             toexec = ['sudo']
  83.  
  84.         # re-exec ourselves first time round, to cope with broken argument
  85.         # handling in kdesu
  86.         toexec.append(sys.argv[0])
  87.         toexec.extend(newargv)
  88.  
  89.     # Workaround for hang on relatively low-memory PS3 systems (#106683).
  90.     # Apparently killing a few processes up-front helps.
  91.     try:
  92.         lowmem = False
  93.         archdetect = subprocess.Popen(['archdetect'], stdout=subprocess.PIPE)
  94.         arch = archdetect.communicate()[0].strip()
  95.         if arch == 'powerpc/ps3':
  96.             meminfo = open('/proc/meminfo')
  97.             try:
  98.                 for line in meminfo:
  99.                     if line.startswith('MemTotal:'):
  100.                         mem = int(line.split()[1])
  101.                         if mem <= 262144:
  102.                             lowmem = True
  103.                             break
  104.             finally:
  105.                 meminfo.close()
  106.         if lowmem:
  107.             syslog.syslog("Low memory: killing processes to work around hang")
  108.             if frontend == 'gtk_ui':
  109.                 for session_process in ('evolution-alarm-notify',
  110.                                         'gnome-cups-icon',
  111.                                         'jockey-gtk',
  112.                                         'update-notifier'):
  113.                     subprocess.call(['gnome-session-remove',
  114.                                      session_process])
  115.             if os.getuid() == 0:
  116.                 subprocess.call(['/etc/init.d/cupsys', 'stop'])
  117.                 subprocess.call(['/etc/init.d/hplip', 'stop'])
  118.             else:
  119.                 subprocess.call(['sudo', '/etc/init.d/cupsys', 'stop'])
  120.                 subprocess.call(['sudo', '/etc/init.d/hplip', 'stop'])
  121.     except (KeyboardInterrupt, SystemExit):
  122.         raise
  123.     except:
  124.         pass
  125.  
  126.     if 'UBIQUITY_WRAPPER_DEBUG' in os.environ:
  127.         print >>sys.stderr, toexec
  128.     os.execvp(toexec[0], toexec)
  129.     sys.exit(127)
  130.  
  131. if __name__ == '__main__':
  132.     main()
  133.